<?php
require_once '../dao/SignDao.php';
require_once '../dao/MaskerDao.php';
require_once '../entity/Sign.php';

$action = "";
if (isset($_GET['action'])) {
	$action = $_GET['action'];
}

switch ($action) {
	case 'insertJobSign' :
		insertJobSign();
		break;
	case 'insertSign' :
		insertSign();
		break;
	case 'deleteSign' :
		deleteSign();
		break;
	case 'globalSigns' :
		globalSigns();
		break;
	case 'mineSigns' :
		profileSigns();
		break;
	case 'getSign' :
		getSign();
		break;
	case 'profileSigns' :
		profileSigns();
		break;
	case 'watchersSigns' :
		watchersSigns();
		break;
	case 'insertColorfeelChoosed' :
		insertColorfeelChoosed();
		break;
	case 'repliesSigns' :
		repliesSigns();
		break;
	default :
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no action found";
		print json_encode($jsonResult);
		break;
}
/*
 * Permit to insert a job sign
 */
function insertJobSign($name_masker, $text) {
	$signDao = new SignDao();
	$maskerDao = new MaskerDao();
	$jsonResult = array();

	// check if text and id_masker are isset
	if ($name_masker == null || $name_masker == "") {
		return "no name_masker found";
	} else if ($text == null || $text == "") {
		return "no text found";
	} else {
		$masker = $maskerDao-> getMaskerByName($name_masker);
		$idMasker = $masker["id"];
		
		// fill the sign
		$sign = new Sign();
		$sign -> id_masker = $idMasker;
		$sign -> text = $text;
		
		$result = $signDao -> insertSign($sign);
		$jsonResult['result'] = ($result);
	}

	print json_encode($jsonResult);
}

/*
 * Permit to insert a sign
 */
function insertSign() {
	$signDao = new SignDao();
	$jsonResult = array();

	// check if text and id_masker are isset
	if (!isset($_POST['id_masker'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_masker found";
	} else if (!isset($_POST['text'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no text found";
	} else if ($_POST['text'] == '') {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no empty text";
	} else if (strlen ($_POST['text']) > 250) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "text too long";
	} else if (strlen ($_POST['text']) == 0) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "text too short";
	} else {
		$text = $_POST['text'];
		// TODO clean text script?
		$idMasker = $_POST['id_masker'];
		$sign = new Sign();

		// fill the sign
		$sign -> id_masker = $idMasker;
		$sign -> text = $text;
		
		if(isset($_POST['id_sign_father'])) {
			$sign -> id_sign_father = $_POST['id_sign_father'];
		}	

		$result = $signDao -> insertSign($sign);
		$jsonResult['result'] = ($result);
	}

	print json_encode($jsonResult);
}

/*
 * Permit to delete a sign
 */
function deleteSign() {
	// TODO check if id_masker is the logged masker
	$signDao = new SignDao();
	$jsonResult = array();

	// check if text and id_masker are isset
	if (!isset($_POST['id_masker'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_masker found";
	} else if (!isset($_POST['id_sign'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_sign found";
	} else {
		
		$idMasker = $_POST['id_masker'];
		$idSign = $_POST['id_sign'];

		$result = $signDao -> deleteSign($idSign, $idMasker);
		$jsonResult['result'] = ($result);
	}

	print json_encode($jsonResult);
}

/*
 * Permit to choose a colorfeel of a sign
 */
function insertColorfeelChoosed() {
	$signDao = new SignDao();
	$jsonResult = array();

	// check if text and id_masker are isset
	if (!isset($_POST['sign_id'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no sign_id found";
	} if (!isset($_POST['id_masker_from'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_masker_from found";
	} else if (!isset($_POST['color'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no color found";
	} else {
		$color = $_POST['color'];
		$signId = $_POST['sign_id'];
		$idMaskerFrom = $_POST['id_masker_from'];
		
		$result = $signDao -> insertColorfeelChoosed($signId, $color, $idMaskerFrom);
		$jsonResult['result'] = ($result);
	}

	print json_encode($jsonResult);
}

/*
 * Permit to retrieve global signs
 */
function globalSigns() {
	$signDao = new SignDao();
	$jsonResult = array();
	$limitFrom = 0;
	$limitTo = 10;
	$lastId = null;
	if (isset($_POST['limit_from'])) {
		$limitFrom = $_POST['limit_from'];
	}

	if (isset($_POST['limit_to'])) {
		$limitTo = $_POST['limit_to'];
	}
	if (isset($_POST['last_id'])) {
		$lastId = $_POST['last_id'];
	}

	$value = $signDao -> getGlobalSignList($limitFrom, $limitTo, $lastId);
	$jsonResult['value'] = ($value);

	print json_encode($jsonResult);
}

/*
 * Permit to retrieve signs of a masker
 */
function profileSigns() {
	$signDao = new SignDao();
	$jsonResult = array();
	$limitFrom = 0;
	$limitTo = 10;
	$lastId = null;
	if (isset($_POST['limit_from'])) {
		$limitFrom = $_POST['limit_from'];
	}
	if (isset($_POST['limit_to'])) {
		$limitTo = $_POST['limit_to'];
	}
	if (isset($_POST['last_id'])) {
		$lastId = $_POST['last_id'];
	}
	

	// check if id_masker is isset
	if (!isset($_POST['id_masker'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_masker found";
	} else {
		$idMasker = $_POST['id_masker'];
		$value = $signDao -> getMineSignList($limitFrom, $limitTo, $idMasker, $lastId);
		$jsonResult['value'] = ($value);
	}
	print json_encode($jsonResult);
}

/*
 * Permit to retrieve watchers signs
 */
function watchersSigns() {
	$signDao = new SignDao();
	$jsonResult = array();
	$limitFrom = 0;
	$limitTo = 10;
	$lastId = null;
	if (isset($_POST['limit_from'])) {
		$limitFrom = $_POST['limit_from'];
	}
	if (isset($_POST['limit_to'])) {
		$limitTo = $_POST['limit_to'];
	}
	if (isset($_POST['last_id'])) {
		$lastId = $_POST['last_id'];
	}
	

	// check if id_masker is isset
	if (!isset($_POST['id_masker'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_masker found";
	} else {
		$idMasker = $_POST['id_masker'];
		$value = $signDao -> getWachersSignList($limitFrom, $limitTo, $idMasker, $lastId);
		$jsonResult['value'] = ($value);
	}
	print json_encode($jsonResult);
}

/*
 * Permit to retrieve replies sign of a sign
 */
function repliesSigns() {
	$signDao = new SignDao();
	$jsonResult = array();
	$limitFrom = 0;
	$limitTo = 10;
	$lastId = null;
	if (isset($_POST['limit_from'])) {
		$limitFrom = $_POST['limit_from'];
	}
	if (isset($_POST['limit_to'])) {
		$limitTo = $_POST['limit_to'];
	}
	if (isset($_POST['last_id'])) {
		$lastId = $_POST['last_id'];
	}
	

	// check if id_masker is isset
	if (!isset($_POST['id_sign_father'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_sign_father found";
	} else {
		$signIdFather = $_POST['id_sign_father'];
		$value = $signDao -> getRepliesSignList($limitFrom, $limitTo, $signIdFather, $lastId);
		$jsonResult['value'] = ($value);
	}
	print json_encode($jsonResult);
}

/*
 * Permit to retrieve a single sign
 */
function getSign() {
	$signDao = new SignDao();
	$jsonResult = array();

	// check if id_sign is isset
	if (!isset($_POST['id_sign'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no id_sign found";
	} else {
		$idSign = $_POST['id_sign'];
		$value = $signDao -> getSign($idSign);
		$jsonResult['value'] = ($value);
	}
	print json_encode($jsonResult);
}

/*
 * Permit to insert a job sign
 */
 /*
function insertJobSign() {
	$secretWord = "bababalengu"; // TAKE IT OUTSIDE! TODO
	$signDao = new SignDao();
	$maskerDao = new MaskerDao();
	$jsonResult = array();

	// check if text and id_masker are isset
	if (!isset($_GET['name_masker'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no name_masker found";
	} else if (!isset($_GET['text'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no text found";
	} else if (!isset($_GET['secret'])) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "no secret found";
	}  else if ($_GET['secret'] != $secretWord) {
		$jsonResult['result'] = "ERROR";
		$jsonResult['message'] = "secret not valid";
	} else {
		$name_masker = $_GET['name_masker'];
		$text = $_GET['text'];
		
		return insertJobSign($name_masker, $text);
	}

	print json_encode($jsonResult);
}
*/
?>